package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CheckItemServiceImpl
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/10/13 9:54
 * @Version V1.0
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleDao roleDao;

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        // 分页插件
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page = roleDao.findPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Integer[] ids,Integer[] menuIds, Role role) {
        roleDao.add(role);
        addRoleAndPermission(ids,role.getId());
        addRoleAndMenu(menuIds,role.getId());
    }



    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        return roleDao.findPermissionIdsByRoleId(id);
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id){ return roleDao.findMenuIdsByRoleId(id);}


    @Override
    public void edit(Role role, Integer[] permissionIds,Integer[] menuIds) {
        // 1：获取检查组的基本信息，执行更新
        roleDao.edit(role);
        // 2：使用检查组的id，删除中间表的数据
        roleDao.deleteRoleAndPermissionByPermissionIds(role.getId());
        // 3：重新遍历checkitemIds的数组，获取检查项的id，同时和检查组的id，向检查项和检查组的中间表插入多行数据
        addRoleAndPermission(permissionIds,role.getId());
        roleDao.deleteRoleAndMenuIds(role.getId());
        addRoleAndMenu(menuIds,role.getId());
    }

    @Override
    public void delete(Integer id) {
        // 在删除之前，判断检查组和检查项中间表中是否存在数据，有数据不能删除，没有数据可以删除
        long count = roleDao.findRoleAndPermissionByPermissionId(id);
        long menuCount=roleDao.findRoleAndMenuByMenuId(id);
        // 此时存在数据，不能删除，提示用户
        if(count>0||menuCount>0){
            throw new RuntimeException("中间表和检查组中存在关联数据，不能删除");
        }
        // 删除检查项
        roleDao.deleteById(id);
    }

    @Override
    public List<CheckItem> findAll() {
        return roleDao.findAll();
    }

    @Override
    public List<Menu> findAllForMenu() {
        return roleDao.findAllForMenu();
    }

    private void addRoleAndPermission(Integer[] ids, Integer role_id) {
        for(Integer permission_Id:ids){
            Map<String,Integer> map=new HashMap<>();
            map.put("role_id",role_id);
            map.put("permission_id",permission_Id);
            roleDao.addRoleAndPermission(map);
        }
    }
    private void addRoleAndMenu(Integer[] menuIds, Integer role_id) {
        for(Integer menu_id:menuIds){
            Map<String,Integer> map=new HashMap<>();
            map.put("role_id",role_id);
            map.put("menu_id",menu_id);
            roleDao.addRoleAndMenu(map);
        }
    }

}
